/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.admin;

import com.jfinal.aop.Inject;
import io.jboot.utils.DateUtil;
import io.jboot.utils.ModelUtil;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.model.SiteInfo;
import io.jpress.service.SiteInfoService;
import io.jpress.service.UtmService;
import io.jpress.web.base.AdminControllerBase;

import java.util.List;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 */
@RequestMapping(value = "/admin/site/statistics", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _SiteStatisticsController extends AdminControllerBase {

    @Inject
    private UtmService utmService;

    @Inject
    private SiteInfoService siteInfoService;


    @AdminMenu(text = "站点访问排行", groupId = JPressConsts.SYSTEM_MENU_STATISTICS, order = 0)
    public void index() {

        List<SiteInfo> allSites = ModelUtil.copy(siteInfoService.findAll());

        SiteInfo mainSite = new SiteInfo();
        mainSite.setName("主站点");
        mainSite.setSiteId(0L);
        allSites.add(mainSite);


        for (SiteInfo siteInfo : allSites) {
            Long weekPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisWeek(), null);
            Long monthPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisMonth(), null);
            Long quarterPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisQuarter(), null);
            Long yearPV = utmService.queryPvBySiteId(siteInfo.getSiteId(), DateUtil.getStartOfThisYear(), null);

            siteInfo.put("weekPV",weekPV);
            siteInfo.put("monthPV",monthPV);
            siteInfo.put("quarterPV",quarterPV);
            siteInfo.put("yearPV",yearPV);
        }

        allSites.sort((o1, o2) -> (int) (o2.getLong("weekPV") - o1.getLong("weekPV")));
        setAttr("weekSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("monthPV") - o1.getLong("monthPV")));
        setAttr("monthSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("quarterPV") - o1.getLong("quarterPV")));
        setAttr("quarterSites",ModelUtil.copy(allSites));

        allSites.sort((o1, o2) -> (int) (o2.getLong("yearPV") - o1.getLong("yearPV")));
        setAttr("yearSites",ModelUtil.copy(allSites));

        render("site/site_statistics.html");
    }


}
